package com.yunxi.controller.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yunxi.base.pojo.BaseApplyLevelVO;
import com.yunxi.base.service.BaseApplyLevelDubboService;
import com.yunxi.common.config.RedisCacheService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.RoleVO;
import com.yunxi.sys.service.RoleDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxinxin on 2018/10/20.
 */
@RestController
@RequestMapping("/admin/role")
@Api(tags = "sys:role", description = "角色接口")
public class RoleController {
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleDubboService roleDubboService;
    @Resource
    private RedisCacheService rcs;

    @Autowired
    BaseApplyLevelDubboService baseApplyLevelDubboService;
    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询角色列表")
    public ApiResult page(RoleVO roleVO,Integer pageNum,Integer pageSize) throws Exception {
        PageInfo list = roleDubboService.pageByVo(roleVO,pageNum,pageSize);
        return ResponseUtil.ok(list);
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询角色列表")
    public ApiResult list(RoleVO roleVO) throws Exception {
        return ResponseUtil.ok(roleDubboService.listByVo(roleVO));
    }

    /**
     * 读取角色信息
     *
     * @param params
     * @return
     * @throws Exception
     */
    @GetMapping("/load")
    @ApiOperation(value = "读取角色信息")
    public Object load(@RequestParam Map<String, String> params) throws Exception {
        return ResponseUtil.ok("成功", roleDubboService.getById((String) params.get("id")));
    }

    /**
     * 保存角色信息
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存角色")
    public ApiResult save(@RequestBody RoleVO role,@CurrentUser LoginInfo loginInfo) throws Exception {
        roleDubboService.saveOrUpdate(role,loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除角色")
    public ApiResult del(@RequestBody List<RoleVO> roleList) throws Exception {
        roleDubboService.removeByList(roleList);
        rcs.deleteKeys("role_");
        return ResponseUtil.ok("success");
    }

    @GetMapping(value = "/getBaseApplyLevel")
    @ApiOperation(value = "得到个人审批权限")
    public ApiResult getBaseApplyLevel(@CurrentUser LoginInfo loginInfo) throws Exception {
        BaseApplyLevelVO baseApplyLevelVO = new BaseApplyLevelVO();
        baseApplyLevelVO.setBusId(Constant.PMS_VERIFY_RETUR_VEHICLE);
        baseApplyLevelVO.setUserCode(loginInfo.getUserCode());
        List<BaseApplyLevelVO> baseApplyLevelVOList = baseApplyLevelDubboService.listByVo(baseApplyLevelVO);
        if(baseApplyLevelVOList != null && baseApplyLevelVOList.size() > 0){
            loginInfo.setCurrentLevel(baseApplyLevelVOList.get(0).getCurrentLevel());
        }
        return ResponseUtil.ok(loginInfo);
    }
}
